package com.example.javaairportflightanalysis.dao.sql;

import io.micrometer.common.util.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author shen
 */
public class DfdlSqlProvider {

    public String getA2401() {
        return new SQL(){{
            SELECT_DISTINCT("DAA.APNO,MDDD.DFLT_CFNO,MDDD.DFLT_FLNO, MDDD.DFLT_AWCD,DAA.APCD,MDDD.DTIME,DAA.FPTT,DAA.FPLT" +
                    ",DAA.APAT" +
                    ",MDDD.DFLT_FLID"+
                    ",MDDD.AIRL_ARPT_ID")
            .FROM("zbtj.vw_MSG_DFOE_DFDL_DFLT MDDD")
            .JOIN("zbtj.\"DFOE_AIRL_ARPT\" DAA ON MDDD.AIRL_ARPT_ID= DAA.AIRL_ARPT_ID")
            .WHERE("MDDD.DFLT_FLIO = 'A' AND DAA.APAT=2401");
        }}.toString();
    }

    public String selectD2401(String dfltFlno,String dtime,String dfltAwcd,String apcd,String fptt,String fplt){
        return new SQL(){{
            SELECT("DAA.APNO,MDDD.DFLT_CFNO,MDDD.DFLT_FLNO, MDDD.DFLT_AWCD,DAA.APCD,MDDD.DTIME,DAA.FPTT,DAA.FPLT" +
                                ",MDDD.AIRL_ARPT_ID" +
                                ",DAA.APAT"  +
                                ",MDDD.AIRL_ARPT_ID ");
            FROM("zbtj.vw_MSG_DFOE_DFDL_DFLT MDDD");
                    JOIN("zbtj.\"DFOE_AIRL_ARPT\" DAA ON MDDD.AIRL_ARPT_ID= DAA.AIRL_ARPT_ID");
            if (StringUtils.isNotBlank(dfltFlno)) {
                WHERE("MDDD.DFLT_FLNO like '%${dfltFlno}%'");
            }
           if (StringUtils.isNotBlank(dfltAwcd)){
               WHERE("MDDD.DFLT_AWCD = #{dfltAwcd}");
           }
            if (StringUtils.isNotBlank(apcd)) {
                WHERE("DAA.APCD  like '%${apcd}%'");
            }
            if (StringUtils.isNotBlank(dtime)){
                WHERE("MDDD.DTIME like '%${dtime}%'");
            }
            if (StringUtils.isNotBlank(fplt) ||StringUtils.isNotBlank(fptt)){
                WHERE ("DAA.FPTT between #{fptt} and #{fplt}");
            }
                WHERE("MDDD.DFLT_FLIO = 'D'");
        }}.toString();
    }

    public String selectA2401(String dfltFlno, String dtime,String dfltAwcd, String apcd, String fptt, String fplt){
        return new SQL(){{
            SELECT("DAA.APNO,MDDD.DFLT_CFNO,MDDD.DFLT_FLNO, MDDD.DFLT_AWCD,DAA.APCD,MDDD.DTIME,DAA.FPTT,DAA.FPLT" +
                    ",MDDD.AIRL_ARPT_ID" +
                    ",DAA.APAT"  +
                    ",MDDD.AIRL_ARPT_ID" +
                    ",MDDD.DFLT_FLID");
            FROM("zbtj.vw_MSG_DFOE_DFDL_DFLT MDDD");
            JOIN("zbtj.\"DFOE_AIRL_ARPT\" DAA ON MDDD.AIRL_ARPT_ID= DAA.AIRL_ARPT_ID");
            if (StringUtils.isNotBlank(dfltFlno)) {
                WHERE("MDDD.DFLT_FLNO like '%${dfltFlno}%'");
            }
            if (StringUtils.isNotBlank(dfltAwcd)){
                WHERE("MDDD.DFLT_AWCD like '%${dfltAwcd}%'");
            }
            if (StringUtils.isNotBlank(dtime)){
                WHERE("MDDD.DTIME like '%${dtime}%'");
            }
            if (StringUtils.isNotBlank(apcd)) {
                WHERE("DAA.APCD = #{apcd}");
            }
            if (StringUtils.isNotBlank(fplt) ||StringUtils.isNotBlank(fptt)){
                WHERE ("DAA.FPTT between #{fptt} and #{fplt}");
            }
            WHERE("MDDD.DFLT_FLIO = 'A'");
        }}.toString();
    }

}
